package cn.yd.rb.service.serviceimpl;


import cn.yd.rb.mapper.ChapterMapper;
import cn.yd.rb.mapper.NovelMapper;
import cn.yd.rb.mapper.TypeMapper;
import cn.yd.rb.po.*;
import cn.yd.rb.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class NovelServiceImpl implements NovelService {

    @Autowired
    private NovelMapper novelMapper;

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private ChapterMapper chapterMapper;
    @Override
    public List<Type> findNoveltype() {
        TypeExample typeExample = new TypeExample();
        typeExample.setOrderByClause("path_id asc");
        List<Type> novelType= typeMapper.selectByExample(typeExample);
        return novelType;
    }

    @Override
    public PageBean<Novel> findNovelAll(Integer pc, Integer ps, NovelQueryVo novelQueryVo) {
        PageBean<Novel> pb = new PageBean<Novel>();
        PageHelper.startPage(pc, ps);//计算分页数据

        NovelExample novelExample = new NovelExample();
        NovelExample.Criteria criteria = novelExample.createCriteria();
        //添加条件
        criteria.andIsDeleteNotEqualTo(1);
        novelExample.setOrderByClause("id desc");

        //存放条件
        String searchStr = "";

        //判断查询条件是否为null
        if(novelQueryVo.getNovel()!=null){
            //判断是否传入了名称
            if(novelQueryVo.getNovel().getNovelname()!=null && !"".equals(novelQueryVo.getNovel().getNovelname())){
                criteria.andNovelnameLike("%"+novelQueryVo.getNovel().getNovelname()+"%");
                searchStr += "&novel.novelname="+novelQueryVo.getNovel().getNovelname();
            }
            //判断是否传入了类型
            if(novelQueryVo.getNovel().getTypeId()!=null && !"".equals(novelQueryVo.getNovel().getTypeId())){
                criteria.andTypeIdEqualTo(novelQueryVo.getNovel().getTypeId());
                searchStr += "&novel.typeId="+novelQueryVo.getNovel().getTypeId();
            }
            //判断是否传入了免费/会员
            if(novelQueryVo.getNovel().getIsFree()!=null && !"".equals(novelQueryVo.getNovel().getIsFree())){
                criteria.andIsFreeEqualTo(novelQueryVo.getNovel().getIsFree());
                searchStr += "&novel.isFree="+novelQueryVo.getNovel().getIsFree();
            }
        }
        pb.setUrl(searchStr);

        List<Novel> list = novelMapper.selectByExample(novelExample);
        pb.setBeanList(list);//将当前页显示的记录存入集合

        PageInfo<Novel> info = new PageInfo<Novel>(list);
        pb.setInfo(info);

        return pb;
    }

    @Override
    public List<Novel> findNovelAll(Integer typeId) {
        NovelExample novelExample = new NovelExample();
        NovelExample.Criteria criteria = novelExample.createCriteria();
        criteria.andIsDeleteNotEqualTo(1);
        criteria.andTypeIdEqualTo(typeId);
        novelExample.setOrderByClause("id asc limit 10");
        List<Novel> novelList= novelMapper.selectByExample(novelExample);
        return novelList;
    }

    @Override
    public List<Novel> findNovelHot() {
        NovelExample novelExample = new NovelExample();
        NovelExample.Criteria criteria = novelExample.createCriteria();
        criteria.andDcountsGreaterThan(50);
        criteria.andIsDeleteNotEqualTo(1);
        novelExample.setOrderByClause("dcounts desc limit 6");
        List<Novel> novelListHot= novelMapper.selectByExample(novelExample);
        return novelListHot;
    }

    @Override
    public List<Novel> findNovelSlider() {
        NovelExample novelExample = new NovelExample();
        NovelExample.Criteria criteria = novelExample.createCriteria();
        criteria.andIsDeleteNotEqualTo(1);
        novelExample.setOrderByClause("id asc limit 6");
        List<Novel> novelSliderList= novelMapper.selectByExample(novelExample);
        return novelSliderList;
    }

    @Override
    public List<Novel> findNovelVip(String str) {
        NovelExample novelExample = new NovelExample();
        NovelExample.Criteria criteria = novelExample.createCriteria();
        criteria.andIsDeleteNotEqualTo(1);
        if(str.equals("免费")){
            criteria.andIsFreeEqualTo(0);
        }else{
            criteria.andIsFreeEqualTo(1);
        }
        novelExample.setOrderByClause("id asc");
        List<Novel> novelVipList= novelMapper.selectByExample(novelExample);
        return novelVipList;
    }

    @Override
    public List<Novel> findNovelRec() {
        NovelExample novelExample = new NovelExample();
        NovelExample.Criteria criteria = novelExample.createCriteria();
        criteria.andIsDeleteNotEqualTo(1);
        novelExample.setOrderByClause("avgscore desc,id asc limit 4");
        List<Novel> novelRemList= novelMapper.selectByExample(novelExample);
        return novelRemList;
    }

    @Override
    public List<Novel> findNovelDowm() {
        NovelExample novelExample = new NovelExample();
        NovelExample.Criteria criteria = novelExample.createCriteria();
        criteria.andIsDeleteNotEqualTo(1);
        novelExample.setOrderByClause("dcounts desc,avgscore asc limit 8");
        List<Novel> novelDownList= novelMapper.selectByExample(novelExample);
        return novelDownList;
    }

    @Override
    public Novel findNovelById(Integer id) {
        NovelExample novelExample = new NovelExample();
        NovelExample.Criteria criteria = novelExample.createCriteria();
        criteria.andIsDeleteNotEqualTo(1);
        Novel novel= novelMapper.selectByPrimaryKey(id);
        return novel;
    }

    @Override
    public Integer findNovelCounts() {
        NovelExample novelExample=new NovelExample();
        Integer novelCounts=novelMapper.selectByExample(novelExample).size();
        return novelCounts;
    }

    @Override
    public void logicDeleteNovel(List<Integer> ids) {

        for (Integer id: ids) {
            Novel novel=novelMapper.selectByPrimaryKey(id);
            novel.setIsDelete(1);
            novelMapper.updateByPrimaryKey(novel);
        }
    }

    @Override
    public PageBean<Novel> findDeletedNovelAll(Integer pc, Integer pageSiez, NovelQueryVo novelQueryVo) {
        PageBean<Novel> pb = new PageBean<Novel>();
        PageHelper.startPage(pc, pageSiez);//计算分页数据

        NovelExample novelExample = new NovelExample();
        NovelExample.Criteria criteria = novelExample.createCriteria();
        //添加条件
        criteria.andIsDeleteNotEqualTo(0);
        novelExample.setOrderByClause("id desc");

        //存放条件
        String searchStr = "";

        //判断查询条件是否为null
        if(novelQueryVo.getNovel()!=null){
            //判断是否传入了名称
            if(novelQueryVo.getNovel().getNovelname()!=null && !"".equals(novelQueryVo.getNovel().getNovelname())){
                criteria.andNovelnameLike("%"+novelQueryVo.getNovel().getNovelname()+"%");
                searchStr += "&novel.novelname="+novelQueryVo.getNovel().getNovelname();
            }
            //判断是否传入了类型
            if(novelQueryVo.getNovel().getTypeId()!=null && !"".equals(novelQueryVo.getNovel().getTypeId())){
                criteria.andTypeIdEqualTo(novelQueryVo.getNovel().getTypeId());
                searchStr += "&novel.typeId="+novelQueryVo.getNovel().getTypeId();
            }
            //判断是否传入了免费/会员
            if(novelQueryVo.getNovel().getIsFree()!=null && !"".equals(novelQueryVo.getNovel().getIsFree())){
                criteria.andIsFreeEqualTo(novelQueryVo.getNovel().getIsFree());
                searchStr += "&novel.isFree="+novelQueryVo.getNovel().getIsFree();
            }
        }
        pb.setUrl(searchStr);

        List<Novel> list = novelMapper.selectByExample(novelExample);
        pb.setBeanList(list);//将当前页显示的记录存入集合

        PageInfo<Novel> info = new PageInfo<Novel>(list);
        pb.setInfo(info);

        return pb;
    }

    @Override
    public void recoverNovel(List<Integer> arr) {
        for (Integer id:arr) {
            Novel novel=novelMapper.selectByPrimaryKey(id);
            novel.setIsDelete(0);
            novelMapper.updateByPrimaryKey(novel);
        }
    }

    @Override
    public void addNewNovel(Novel novel) {
        novel.setDcounts(0);
        novel.setCommentcounts(0);
        novel.setIsDelete(0);
        novel.setAvgscore((float) 0);
        novelMapper.insertSelective(novel);
    }

    @Override
    public void updateNovelTxt(Novel novel) {
        novelMapper.updateByPrimaryKeySelective(novel);
    }

    @Override
    public List<Chapter> findChapterAllById(Integer novel_id) {
        ChapterExample chapterExample  = new ChapterExample();
        ChapterExample.Criteria criteria = chapterExample.createCriteria();
        criteria.andNovelIdEqualTo(novel_id);
        List<Chapter> chapterList = chapterMapper.selectByExample(chapterExample);
        return chapterList;
    }

    @Override
    public void addCounts(Integer id) {
        NovelExample novelExample = new NovelExample();

        Novel novel= novelMapper.selectByPrimaryKey(id);
        Integer count = novel.getDcounts()+1;
        novel.setDcounts(count);
        novelMapper.updateByPrimaryKeySelective(novel);
    }
}
